package com.sgb.kjwl.view.ui.adapter.transport;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.InquiryEntity;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.UserOperating;

import java.util.List;

/**
 * @Author : 熊丽
 * @date :2021/4/6 0006$ 11:47$
 * @Description :报名货主和司机询价管理列表适配器$
 */
public class OwnerAndDriverInquiryAdapter extends BaseQuickAdapter<InquiryEntity, BaseViewHolder> {
private boolean isOwner;
    private InquiryClickListener mListener;
    public OwnerAndDriverInquiryAdapter(@Nullable List<InquiryEntity> data) {
        super(R.layout.owner_and_driver_inquiry_item, data);
    }
    public void setOwner(boolean isOwner){
        this.isOwner = isOwner;
    }
    public interface InquiryClickListener {
        void btnGoClick(String s, InquiryEntity item);
        void OnItemClicik(InquiryEntity inquiryEntity);
        void OnPhoneCall(InquiryEntity inquiryEntity);
    }
    public void SetOrderListener(InquiryClickListener listener){
        this.mListener = listener;
    }
    @Override
    protected void convert(BaseViewHolder helper, InquiryEntity item) {
        helper.setVisible(R.id.ll_owner_load_time, isOwner ? false : true);
        helper.setVisible(R.id.ll_driver_load_time, isOwner ? true : false);
        helper.setVisible(R.id.ll_vehicle_information, isOwner ? true : false);
        helper.setVisible(R.id.ll_inquiry_owner, isOwner ? true : false);
        helper.setVisible(R.id.show_agree_offer, isOwner ? false : true);
        helper.setVisible(R.id.ll_inquiry_freight,isOwner?false:true);
        setDetail(item,helper);
        setState(item,helper);
    }
    private void setState(InquiryEntity item, BaseViewHolder helper){
            helper.setVisible(R.id.iv_item_my_inquiry_status,true);
        switch (item.getOfferStatus()){
            case  1://待确认
                if (isOwner){
                    helper.setText(R.id.tv_item_my_inquiry_status,"询价待确认");
                    helper.setTextColor(R.id.tv_item_my_inquiry_status, Color.parseColor("#5792FD"));
                }else {
                    helper.setBackgroundRes(R.id.iv_item_my_inquiry_status,isOwner? R.drawable.driver_sure: R.drawable.owner_sure);
                    if(UserOperating.getInstance().getUserIdentity()==1||UserOperating.getInstance().getUserIdentity()==2){
                        helper.getView(R.id.show_agree_offer).setVisibility(View.GONE);
                    }else{
                        helper.setText(R.id.tv_item_my_offer_btn,"同意询价");
                        helper.getView(R.id.show_agree_offer).setVisibility(View.VISIBLE);
                    }
                }
                break;
            case 2://已同意
                if (isOwner){
                    helper.setText(R.id.tv_item_my_inquiry_status,"同意询价");
                    helper.setTextColor(R.id.tv_item_my_inquiry_status, Color.parseColor("#06C764"));
                }else {
                    helper.setBackgroundRes(R.id.iv_item_my_inquiry_status,isOwner? R.drawable.driver_agree: R.drawable.owner_agree);
                }

                if (!isOwner){
                    helper.setText(R.id.tv_item_my_offer_btn,"查看运输订单");
                }
                break;
            case 3://超时未确认
                if (isOwner){
                    helper.setText(R.id.tv_item_my_inquiry_status,"超时未确认");
                    helper.setTextColor(R.id.tv_item_my_inquiry_status, Color.parseColor("#FF2928"));
                }else {
                    helper.setBackgroundRes(R.id.iv_item_my_inquiry_status,isOwner? R.drawable.driver_not_sure: R.drawable.owner_not_sure);
                }
                helper.setVisible(R.id.show_agree_offer,false);

                break;
            case 4://已过期
                if (isOwner){
                    helper.setText(R.id.tv_item_my_inquiry_status,"询价已过期");
                    helper.setTextColor(R.id.tv_item_my_inquiry_status, Color.parseColor("#5792FD"));
                }else {
                    helper.setBackgroundRes(R.id.iv_item_my_inquiry_status,isOwner? R.drawable.driver_not_sure: R.drawable.owner_not_sure);
                }
                helper.setVisible(R.id.show_agree_offer,false);

                break;
        }
    }
    private void setDetail(InquiryEntity item,BaseViewHolder helper){

        helper.setText(R.id.tv_item_my_offer_place,item.getCityName()+" "+item.getDistrictName());
        helper.setText(R.id.tv_item_my_offer_place_dest,item.getDestCityName()+" "+item.getDestDistrictName());
        if (!isOwner){
            helper.setText(R.id.tv_item_my_offer_goods,(CargoConstant.getLastGoodsTypeName(item.getGoodsModelName(),item.getSecondGoodsTypeName())+"/")+(TextUtils.isEmpty(item.getWeight())?"":NumberUtil.trim0(item.getWeight()))+"吨"+""+(TextUtils.isEmpty(item.getVolume())?"":"/"+item.getVolume()+"方"));
            helper.setText(R.id.item_my_offer_freight,"信息费");
            helper.setText(R.id.item_my_offer_msg_price,"运费单价");
            helper.setText(R.id.item_my_offer_price_time, mContext.getResources().getString(R.string.app_the_freight_combined));
            helper.setText(R.id.tv_item_my_offer_goods_time, TextUtils.isEmpty(item.getShipmentTime())?"/":CargoConstant.stampFormatListLoadTime(true,NumberUtil.convertToLong(item.getShipmentTime()), item.getStartPointTime(),NumberUtil.convertToLong(item.getShipmentEndTime()), item.getEndPointTime()));
            helper.setText(R.id.tv_item_my_offer_freight, NumberUtil.trim0(item.getMessagePrice())+"元");
            helper.setText(R.id.tv_item_my_offer_msg_price,NumberUtil.trim0(item.getFreight())+item.getFreightUnit());
            helper.setText(R.id.tv_item_my_offer_price_time,NumberUtil.trim0(item.getAllFreight())+"元");
            helper.setTextColor(R.id.tv_item_my_offer_price_time, Color.parseColor("#FF4B10"));

        }else {
            helper.setVisible(R.id.ll_inquiry_object,TextUtils.isEmpty(item.getCompanyName())?false:true);
            helper.setVisible(R.id.ll_vehicle_information,false);
            helper.setText(R.id.tv_item_my_offer_goods,(CargoConstant.getLastGoodsTypeName(item.getGoodsTypeName(),item.getSecondGoodsTypeName())+"/")+(TextUtils.isEmpty(item.getWeight())?"":NumberUtil.trim0(item.getWeight())+"吨")+(TextUtils.isEmpty(item.getVolume())?"":"/"+item.getVolume()+"方"));
            helper.setText(R.id.item_my_offer_freight,"信息费");
            helper.setText(R.id.item_my_offer_msg_price, mContext.getResources().getString(R.string.app_the_freight_combined));
            helper.setText(R.id.tv_item_my_offer_freight, NumberUtil.trim0(item.getMessagePrice())+"元");
            helper.setText(R.id.tv_item_my_offer_msg_price,NumberUtil.trim0(item.getAllFreight())+"元");
            helper.setText(R.id.tv_driver_load_time,TextUtils.isEmpty(item.getShipmentTime())?"/":TextUtils.isEmpty(item.getShipmentTime())?"/":CargoConstant.stampFormatListLoadTime(true,NumberUtil.convertToLong(item.getShipmentTime()), item.getStartPointTime(),NumberUtil.convertToLong(item.getShipmentEndTime()), item.getEndPointTime()));
            helper.setText(R.id.tv_driver_car_info,item.getCarTypeName()+"  "+item.getCarLenName()+"  "+"栏高 "+NumberUtil.trim0(item.getHeightHurdle())+"米"+"  "+item.getIsDumper());
            helper.setText(R.id.tv_driver_name,TextUtils.isEmpty(item.getName())?"/":item.getName()+" ");
            helper.setText(R.id.tv_driver_number,item.getTelphone());
            helper.setText(R.id.tv_inquiry_object,item.getCompanyName());
            helper.setText(R.id.driver_owner_name,"联系信息");
        }
        TextView btn_go = helper.getView(R.id.tv_item_my_offer_btn);
        helper.getView(R.id.tv_item_my_offer_btn).setOnClickListener(v -> mListener.btnGoClick(btn_go.getText().toString()
                ,item));
        helper.getView(R.id.inquiry_ll_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.OnItemClicik(item);
            }
        });
        helper.getView(R.id.ll_inquiry_owner).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.OnPhoneCall(item);
            }
        });
    }
}
